package com.ruoyi.yipaiju.controller;

import com.google.common.collect.ImmutableMap;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.ApiResult;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.yipaiju.domain.StorageRelationship;
import com.ruoyi.yipaiju.service.IStorageRelationshipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/storage")
public class StorageController extends BaseController {


    @Autowired
    private IStorageRelationshipService storageRelationshipService;

    /**
     * 批量获取用户列表 TODO 这里不想这么干了 直接在房源里反回
     * @param storageRelationship
     * @return
     */
    @PostMapping("/batchGet")
    public ApiResult batchGet(StorageRelationship storageRelationship)
    {
        List<StorageRelationship> storageRelationships = storageRelationshipService.selectStorageRelationshipList(storageRelationship);

        Map<String, List<Map<String, Object>>> imageMap = new HashMap<>();

        storageRelationships.forEach(item -> {
            List<Map<String, Object>> categoryList = imageMap.get(item.getCategory());
            if (categoryList == null){
                categoryList = new ArrayList<>();
                imageMap.put(item.getCategory(), categoryList);
            }
            categoryList.add(ImmutableMap.of("id", item.getId(), "objectName", item.getId(), "url", item.getFilename()));
        });

//        List<Map<String, Object>> bannerList = new ArrayList<>();
//        if (storageRelationships != null){
//            for (StorageRelationship relationship : storageRelationships) {
//                bannerList.add(ImmutableMap.of("id", relationship.getId(), "img", relationship.getFilename(), "url", relationship.getFilename()));
//            }
//        }
        return ApiResult.success(imageMap);
    }

    /**
     * 上传图片
     * @param storageRelationship
     * @param file
     * @return
     */
    @PostMapping("/signature")
    public ApiResult signature(StorageRelationship storageRelationship, MultipartFile file){
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            storageRelationship.setFilename(fileName);
            storageRelationshipService.insertStorageRelationship(storageRelationship);
            return ApiResult.success(ImmutableMap.of("objectName", storageRelationship.getId(), "fileName", fileName));
        }catch (Exception e){
            e.printStackTrace();
        }
        return ApiResult.error();
    }

    @PostMapping("/signature1")
    public ApiResult signature1(MultipartFile file){
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            return ApiResult.success(ImmutableMap.of("objectName", file.getOriginalFilename(), "fileName", fileName));
        }catch (Exception e){
            e.printStackTrace();
        }
        return ApiResult.error();
    }

    /**
     * 删除文件资源
     */
    @DeleteMapping("/{id}")
    public ApiResult remove(@PathVariable Long id) {
        StorageRelationship storageRelationship = storageRelationshipService.selectStorageRelationshipById(id);
        if (storageRelationship != null){
            storageRelationshipService.deleteStorageRelationshipById(id);
            // TODO 同时删除文件
            File file = new File(RuoYiConfig.getUploadPath() + storageRelationship.getFilename());
            if (file.exists()){
                file.delete();
            }
        }
        return ApiResult.success();
    }


}
